﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.CompositeWeb;
using Microsoft.Practices.CompositeWeb.Interfaces;
using Microsoft.Practices.CompositeWeb.Services;
using Microsoft.Practices.CompositeWeb.Configuration;
using Microsoft.Practices.CompositeWeb.EnterpriseLibrary.Services;

namespace EPMPerCenter.Portal
{
    public class PortalModuleInitializer : ModuleInitializer
    {
        private const string AuthorizationSection = "compositeWeb/authorization";

        public override void Load(CompositionContainer container)
        {
            base.Load(container);

            AddGlobalServices(container.Parent.Services);
            AddModuleServices(container.Services);
            RegisterSiteMapInformation(container.Services.Get<ISiteMapBuilderService>(true));

            container.RegisterTypeMapping<IPortalController, PortalController>();
        }

        protected virtual void AddGlobalServices(IServiceCollection globalServices)
        {
            // TODO: add a service that will be visible to any module
        }

        protected virtual void AddModuleServices(IServiceCollection moduleServices)
        {
            // TODO: add a service that will be visible to this module
        }

        protected virtual void RegisterSiteMapInformation(ISiteMapBuilderService siteMapBuilderService)
        {
            //SiteMapNodeInfo moduleNode = new SiteMapNodeInfo("Portal", "~/Portal/Default.aspx", "首页");
            //siteMapBuilderService.AddNode(moduleNode);

            //收入中心
            SiteMapNodeInfo inComeListNode = new SiteMapNodeInfo("InComeList", "~/Portal/InComeList.aspx", "收入中心");
            siteMapBuilderService.AddNode(inComeListNode);
            SiteMapNodeInfo editInComeNode = new SiteMapNodeInfo("EditInCome", "~/Portal/EditInCome.aspx", "添加收入");
            siteMapBuilderService.AddNode(editInComeNode, inComeListNode);

            //支出中心
            SiteMapNodeInfo outComeListNode = new SiteMapNodeInfo("OutComeList", "~/Portal/OutComeList.aspx", "订单中心");
            siteMapBuilderService.AddNode(outComeListNode);
            SiteMapNodeInfo editOutComeNode = new SiteMapNodeInfo("EditOutCome", "~/Portal/EditOutCome.aspx", "添加订单");
            siteMapBuilderService.AddNode(editOutComeNode, outComeListNode);

            //项目中心
            SiteMapNodeInfo projectListNode = new SiteMapNodeInfo("ProjectList", "~/Portal/ProjectList.aspx", "项目中心");
            siteMapBuilderService.AddNode(projectListNode);

            //绩效管理
            SiteMapNodeInfo staffPerfListNode = new SiteMapNodeInfo("StaffPerfList", "~/Portal/StaffPerfList.aspx", "绩效列表");
            siteMapBuilderService.AddNode(staffPerfListNode);
            SiteMapNodeInfo editStaffPerfNode = new SiteMapNodeInfo("EditStaffPerf", "~/Portal/EditStaffPerf.aspx", "添加绩效信息");
            siteMapBuilderService.AddNode(editStaffPerfNode, staffPerfListNode);

            //供应商管理
            SiteMapNodeInfo supplyListNode = new SiteMapNodeInfo("SupplyList", "~/Portal/SupplyList.aspx", "供应商管理");
            siteMapBuilderService.AddNode(supplyListNode);
            SiteMapNodeInfo editSupplyNode = new SiteMapNodeInfo("EditSupply", "~/Portal/EditSupply.aspx", "添加供应商");
            siteMapBuilderService.AddNode(editSupplyNode, supplyListNode);

            //联系人管理
            SiteMapNodeInfo contactListNode = new SiteMapNodeInfo("ContactList", "~/Portal/ContactList.aspx", "联系人管理");
            siteMapBuilderService.AddNode(contactListNode);
            SiteMapNodeInfo editContactNode = new SiteMapNodeInfo("EditContact", "~/Portal/EditContact.aspx", "添加联系人");
            siteMapBuilderService.AddNode(editContactNode, contactListNode);

            //日志管理
            SiteMapNodeInfo logListtNode = new SiteMapNodeInfo("LogList", "~/Portal/LogList.aspx", "日志管理");
            siteMapBuilderService.AddNode(logListtNode);

            //系统配置信息管理
            SiteMapNodeInfo configListNode = new SiteMapNodeInfo("ConfigList", "~/Portal/ConfigList.aspx", "系统配置管理");
            siteMapBuilderService.AddNode(configListNode);
            SiteMapNodeInfo editConfigNode = new SiteMapNodeInfo("EditConfig", "~/Portal/EditConfig.aspx", "添加系统配置");
            siteMapBuilderService.AddNode(editConfigNode, configListNode);

            //帮助信息管理
            SiteMapNodeInfo helpListNode = new SiteMapNodeInfo("HelpList", "~/Portal/HelpList.aspx", "帮助信息管理");
            siteMapBuilderService.AddNode(helpListNode);
            SiteMapNodeInfo editHelpNode = new SiteMapNodeInfo("EditHelp", "~/Portal/EditHelp.aspx", "添加帮助信息");
            siteMapBuilderService.AddNode(editHelpNode, helpListNode);


            

            // TODO: register other site map nodes that Portal module might provide            
        }

        public override void Configure(IServiceCollection services, System.Configuration.Configuration moduleConfiguration)
        {
            IAuthorizationRulesService authorizationRuleService = services.Get<IAuthorizationRulesService>();
            if (authorizationRuleService != null)
            {
                AuthorizationConfigurationSection authorizationSection = moduleConfiguration.GetSection(AuthorizationSection) as AuthorizationConfigurationSection;
                if (authorizationSection != null)
                {
                    foreach (AuthorizationRuleElement ruleElement in authorizationSection.ModuleRules)
                    {
                        authorizationRuleService.RegisterAuthorizationRule(ruleElement.AbsolutePath, ruleElement.RuleName);
                    }
                }
            }
        }
    }
}
